<template>
  <el-dialog
    class="dict-edit"
    :title="title"
    top="50px"
    :append-to-body="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="isVisible"
  >
    <el-form ref="form" :model="dictItem" :rules="rules" label-position="right" label-width="100px">
      <el-form-item label="名称" prop="itemText">
        <el-input v-model="dictItem.itemText" />
      </el-form-item>
      <el-form-item label="数据值" prop="itemValue">
        <el-input v-model="dictItem.itemValue" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="dictItem.remark" />
      </el-form-item>
      <el-form-item label="是否启用" prop="visibleCheck">
        <el-switch
          v-model="dictItem.status"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" plain @click="isVisible = false">
        {{ $t('common.cancel') }}
      </el-button>
      <el-button type="primary" plain :loading="buttonLoading" @click="submitForm">
        {{ $t('common.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'DictItemEdit',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    dict: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      dictItem: this.initDictItem(),
      buttonLoading: false,
      rules: {
        itemText: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' }
        ],
        itemValue: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    }
  },
  mounted() {
  },
  methods: {
    initDictItem() {
      return {
        itemText: '',
        itemValue: '',
        remark: '',
        status: 1
      }
    },
    setData(val) {
      this.dictItem = {
        id: val.id,
        itemText: val.itemText,
        itemValue: val.itemValue,
        remark: val.remark,
        status: val.status
      }
    },
    close() {
      this.$emit('close')
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (!this.dictItem.id) {
            // create
            this.$post('/system/dictItem', {
              dictId: this.dict.id,
              ...this.dictItem
            }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.createSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          } else {
            // update
            this.$put('/system/dictItem', {
              dictId: this.dict.id,
              ...this.dictItem
            }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.updateSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          }
        } else {
          return false
        }
      })
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.$refs.form.clearValidate()
      this.$refs.form.resetFields()
      this.dictItem = this.initDictItem()
    }
  }
}
</script>

<style lang="scss">
  .dict-edit .el-dialog{
    min-width: 500px;
  }
</style>
